using System;
using UnityEngine;
using UnityEngine.UI;
using Voodoo.GameOps;
using Voodoo.Pattern;

public class BonusLevelManager : SingletonMB<BonusLevelManager>
{
	public bool ForceBonusLevel;

	[SerializeField]
	private GameObject _RVIcon;

	[SerializeField]
	private GameObject _noThanksButton;

	[SerializeField]
	private Image _bonusLevelIcon;

	private int m_BonusLevelReturnId = -1;

	public bool CanShowBonusLevelRV { get; private set; }

	public bool IsBonusLevelActive => m_BonusLevelReturnId == SingletonMB<GameManager>.Instance.ReturnCount;

	private int GamesSinceLastBonusLevel
	{
		get
		{
			return NintendoPlayerPrefs.GetInt("bonus_level_game_count", 0);
		}
		set
		{
			NintendoPlayerPrefs.SetInt("bonus_level_game_count", value);
		}
	}

	private int NumberBonusLevelActivated
	{
		get
		{
			return NintendoPlayerPrefs.GetInt("bonus_level_number_activated", 0);
		}
		set
		{
			NintendoPlayerPrefs.SetInt("bonus_level_number_activated", value);
		}
	}

	public void Start()
	{
		ForceBonusLevel = false;
		CanShowBonusLevelRV = false;
		if (Preloader.BonusLevelConfig.IsEnabled)
		{
			GameManager instance = SingletonMB<GameManager>.Instance;
			instance.OnGameStart = (Action)Delegate.Combine(instance.OnGameStart, new Action(OnGameStarted));
			GameManager instance2 = SingletonMB<GameManager>.Instance;
			instance2.OnGameEnd = (Action)Delegate.Combine(instance2.OnGameEnd, new Action(OnGameEnded));
		}
	}

	private void OnGameEnded()
	{
		CanShowBonusLevelRV = false;
	}

	private void OnGameStarted()
	{
		if (IsBonusLevelActive)
		{
			CanShowBonusLevelRV = false;
			return;
		}
		if (SingletonMB<GameManager>.Instance.currentOilUI.OilValue < SingletonMB<GameManager>.Instance.currentOilUI.MaxOil / 3f)
		{
			GamesSinceLastBonusLevel++;
		}
		else
		{
			GamesSinceLastBonusLevel += 2;
		}
		if (GamesSinceLastBonusLevel >= Preloader.BonusLevelConfig.GamesBetweenBonusLevels || ForceBonusLevel)
		{
			GamesSinceLastBonusLevel = 0;
			CanShowBonusLevelRV = true;
			_RVIcon.SetActive(NumberBonusLevelActivated > 0);
			_noThanksButton.SetActive(NumberBonusLevelActivated > 0);
			_bonusLevelIcon.sprite = InventoryManager.Instance.CurrentlySelected(ItemType.Skin).IconBonus;
		}
	}

	public void OnRVClicked()
	{
		if (NumberBonusLevelActivated == 0)
		{
			ActivateBonusLevel();
		}
		else if (SingletonMB<GameManager>.Instance.BetterNoAds)
		{
			ActivateBonusLevel();
		}
		else
		{
			ActivateBonusLevel();
		}
		void ActivateBonusLevel()
		{
			NumberBonusLevelActivated++;
			m_BonusLevelReturnId = SingletonMB<GameManager>.Instance.ReturnCount;
			SingletonMB<GameManager>.Instance.EndGame();
		}
	}
}
